package com.hg.library.ui.gallery.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hg.highglass_library.R;
import com.hg.library.graph.ImageProcess;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by wangjianqiang on 2016/4/22.
 */
public class CategoryAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Integer> categorys = new ArrayList<>();
    private Map<Integer,Bitmap> reflectBitmap = new HashMap<>();
    public CategoryAdapter(Context context) {
        this.context = context;
    }
    private Map<Integer,String> nameMap;
    public void setCategorys(ArrayList<Integer> categorys){
        this.categorys = categorys;
    }
    public void setNameMap(Map<Integer,String> nameMap){
        this.nameMap = nameMap;
    }
    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object getItem(int i) {
        return categorys.get(i % categorys.size());
    }

    @Override
    public long getItemId(int i) {
        return i % categorys.size();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if(view == null){
            view = LayoutInflater.from(context).inflate(R.layout.category_item,null);
            holder = new ViewHolder();
            holder.view1 = (ImageView)view.findViewById(R.id.category_item_1_iv);
            holder.view2 = (ImageView)view.findViewById(R.id.category_item_2_iv);
            holder.name = (TextView)view.findViewById(R.id.category_item_tv);
            view.setTag(holder);
        }else{
            holder = (ViewHolder)view.getTag();
        }
        holder.view1.setImageResource(categorys.get(i % categorys.size()));
        if(reflectBitmap.containsKey(categorys.get(i % categorys.size()))){
            holder.view2.setImageBitmap(reflectBitmap.get(categorys.get(i % categorys.size())));
        }else{
            Bitmap reflectedBitmap = ImageProcess.createHalfReflectedImage(((BitmapDrawable) context.getResources().getDrawable(categorys.get(i % categorys.size()))).getBitmap());
            holder.view2.setImageBitmap(reflectedBitmap);
            reflectBitmap.put(categorys.get(i % categorys.size()),reflectedBitmap);
        }
        if(nameMap != null && nameMap.containsKey(categorys.get(i % categorys.size()))){
            holder.name.setText(nameMap.get(categorys.get(i % categorys.size())));
        }
        view.setAlpha(0.2f);
        view.setPivotX(161);
        view.setPivotY(161);
        return view;
    }
    class ViewHolder{
        ImageView view1;
        ImageView view2;
        TextView  name;
    }
    public void recycle(){
        Iterator iterator = reflectBitmap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            Bitmap bitmap = (Bitmap)entry.getValue();
            bitmap.recycle();
        }
    }
}
